package com.cooba.activitys.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.activitys.VideoDetailsActivity;
import com.cooba.adapter.IntegralDetailsAdapter;
import com.cooba.adapter.VideoHistoryAdapter;
import com.cooba.bean.VideoBeans;
import com.cooba.pullTorefresh.PullToRefreshBase;
import com.cooba.pullTorefresh.PullToRefreshListView;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.Utils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/6.
 */
public class VideoHistoryActivity extends AppCompatActivity {
    private ImageButton mBackbtn;
    private TextView mClear;
    private PullToRefreshListView rListView;
    private ArrayList<VideoBeans.VideoListEntity> mVideoHistoryList;
    private VideoHistoryAdapter mAdapter;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_history);
        mContext=this;
        mVideoHistoryList = new ArrayList<>();
        initView();
        getVideoHistory();
    }

    private void getVideoHistory() {
        mVideoHistoryList= (ArrayList<VideoBeans.VideoListEntity>) CommonUtil.getVideoHistoryList(mContext);
        mAdapter.setList(mVideoHistoryList);
    }

    private void initView() {
        mBackbtn = (ImageButton) findViewById(R.id.video_history_back_ib);
        mClear = (TextView) findViewById(R.id.video_history_clear);
        mBackbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.removeVideoHistory(mContext);
                mVideoHistoryList.clear();
                mAdapter.notifyDataSetChanged();
            }
        });
        initPullTorefresh();
    }
    private void initPullTorefresh() {
        rListView = (PullToRefreshListView) findViewById(R.id.lv_video_history);
        rListView.setPullRefreshEnabled(false);
        rListView.setPullLoadEnabled(false);
        rListView.setScrollLoadEnabled(false); //设置滚动加载可用
        mAdapter = new VideoHistoryAdapter(this, mVideoHistoryList);
        rListView.getRefreshableView().setAdapter(mAdapter);
        rListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉刷新

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉加载

            }
        });
        rListView.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String videoUrl = mAdapter.getList().get(position).getVideo_page_url().toString().replaceAll("\\\\", "");
                String videoTitle = Utils.replaceString(mAdapter.getList().get(position).getTitle());
                String videoDs = Utils.replaceString(mAdapter.getList().get(position).getIntro());
                Intent intent = new Intent(mContext, VideoDetailsActivity.class);
                intent.putExtra("videoUrl", videoUrl);
                intent.putExtra("videoTitle", videoTitle);
                intent.putExtra("videoDs", videoDs);
                startActivity(intent);
            }
        });
    }
}
